package com.crack.nga2.activity.userCenter;

import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.crack.nga2.R;
import com.crack.nga2.custom.PullListView;
import com.crack.nga2.entity.UserInfo;
import com.crack.nga2.utils.GlobalConst;
import com.crack.nga2.utils.HttpParams;
import com.crack.nga2.volley.UserInfoRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserInfoActivity extends AppCompatActivity {
    private PullListView listView;
    private List<String> items = new ArrayList<>();
    private Map<String, String> map = new HashMap<>();
    private InfoAdapter adapter = new InfoAdapter();
    private RequestQueue mQueue;

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finish();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        mQueue = Volley.newRequestQueue(this);
        listView = (PullListView) findViewById(R.id.listView);
        listView.setNoPullUp(true);
        listView.setAdapter(adapter);
        listView.onStartHeadRefresh();
        getData();
    }

    protected String getUrl() {
        return GlobalConst.BASE_URL + "nuke.php?" + getParams().toString();
    }


    protected HttpParams getParams() {
        HttpParams params = new HttpParams();
        params.put("__lib", "ucp");
        params.put("__act", "get");
        params.put("uid", GlobalConst.UID);
        params.put("lite", "js");
        return params;
    }

    private void getData() {
        String url = getUrl();
        mQueue.add(new UserInfoRequest(Request.Method.GET, url,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        listView.onCompleteRefresh();
                    }
                },
                new Response.Listener<UserInfo>() {
                    @Override
                    public void onResponse(UserInfo response) {
                        items.clear();
                        items.addAll(Arrays.asList(getResources().getStringArray(R.array.user_info)));
                        map.put(items.get(1), response.getUsername());
                        map.put(items.get(2), response.getEmail());
                        map.put(items.get(3), "158*****719");
                        map.put(items.get(4), response.getGroup());
                        map.put(items.get(5), String.valueOf(response.getFame() / 10));
                        map.put(items.get(6), String.valueOf(response.getPosts()));
                        adapter.notifyDataSetChanged();
                        listView.onCompleteRefresh();
                    }
                }));
    }

    private class InfoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int position, View view, ViewGroup parent) {
            String item = items.get(position);
            view = View.inflate(UserInfoActivity.this, R.layout.activity_user_info_item, null);
            TextView text = (TextView) view.findViewById(R.id.item);
            TextView value = (TextView) view.findViewById(R.id.value);
            ImageView iv = (ImageView) view.findViewById(R.id.head_photo);
            text.setText(item);
            if (position == 0) {
                value.setVisibility(View.GONE);
                iv.setVisibility(View.VISIBLE);
                iv.setImageResource(R.drawable.photo);
            } else {
                value.setVisibility(View.VISIBLE);
                iv.setVisibility(View.GONE);
                value.setText(map.get(item));
            }
            return view;
        }
    }
}
